package o2o.web.actions.user;

import com.jinyou.utils.common.AjaxResult;
import com.jinyou.utils.common.JYPageUtils;
import com.jinyou.utils.common.ValidateUtil;
import o2o.cache.user.UserInfoMemory;
import o2o.cache.user.UserOnlineImpl;
import o2o.dao.PO.user.UserCooperateApplyInfo;
import o2o.dao.PO.user.UserInfo;
import o2o.dao.VO.user.UserCooperateApplyVO;
import o2o.service.user.UserCooperateApplyService;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.ArrayList;
import java.util.List;
import java.util.stream.Collectors;

@RestController
public class UserCooperateApplyAction {

    @Autowired
    private UserCooperateApplyService userCooperateApplyService;

    /**
     * 获取最后一次的申请
     */
    @RequestMapping("/user/cooperate/get")
    public String userCooperateGetLast(String token, Integer type) {
        UserInfo user = UserInfoMemory.getInstance().getUserInfo(UserOnlineImpl.getInstance().get(token));
        if (user == null) {
            return AjaxResult.errorLogoutResult();
        }
        List<UserCooperateApplyInfo> list = userCooperateApplyService.findByUsernameAndType(user.getUsername(), type);
        UserCooperateApplyInfo applyInfo = null;
        if (ValidateUtil.isAbsList(list)) {
            applyInfo = list.get(0);
        }
        if (applyInfo != null) {
            UserCooperateApplyVO vo = new UserCooperateApplyVO();
            BeanUtils.copyProperties(applyInfo, vo);
            return AjaxResult.successResult(vo);
        }
        return AjaxResult.successResult();
    }

    /**
     * 用户添加或者修改申请
     *
     * @param token
     * @param applyInfo
     * @return
     */
    @RequestMapping("/user/cooperate/create-modify")
    public String userCooperateGetLast(String token, UserCooperateApplyInfo applyInfo) {
        UserInfo user = UserInfoMemory.getInstance().getUserInfo(UserOnlineImpl.getInstance().get(token));
        if (user == null) {
            return AjaxResult.errorLogoutResult();
        }
        applyInfo.setUsername(user.getUsername());
        try {
            userCooperateApplyService.saveOrCreateApplyInfo(applyInfo, user.getUsername());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.errorResult(e.getMessage());
        }
        return AjaxResult.successResult();
    }

    /**
     * 后台获取列表
     *
     * @param token
     * @param city
     * @param county
     * @param type
     * @param status
     * @param params
     * @param page
     * @param size
     * @return
     */
    @RequestMapping("/admin/user/cooperate/list")
    public String userCooperateList(String token, String city, String county, Integer type, Integer status, String params, Integer page, Integer size) {
        UserInfo user = UserInfoMemory.getInstance().getUserInfo(UserOnlineImpl.getInstance().get(token));
        if (user == null) {
            return AjaxResult.errorLogoutResult();
        }
        if (page == null) {
            page = 1;
        }
        if (size == null) {
            size = 1;
        }
        List<UserCooperateApplyInfo> list = userCooperateApplyService.find(city, county, type, status, params);
        if (ValidateUtil.isAbsList(list)) {
            JYPageUtils.PageList pageList = JYPageUtils.getList(list, page, size);
            list = (List<UserCooperateApplyInfo>) pageList.list;
            List<UserCooperateApplyVO> voList = new ArrayList<>();
            if (ValidateUtil.isAbsList(list)) {
                voList = list.stream().map(item -> {
                    UserCooperateApplyVO vo = new UserCooperateApplyVO();
                    BeanUtils.copyProperties(item, vo);
                    return vo;
                }).collect(Collectors.toList());
            }
            return AjaxResult.successResult(voList, pageList.totalCount);
        }
        return AjaxResult.successResult(null, 0);
    }

    /**
     * 标记为已经处理
     *
     * @param token
     * @param id
     * @return
     */
    @RequestMapping("/admin/user/cooperate/deal")
    public String userCooperateDeal(String token, Long id) {
        UserInfo user = UserInfoMemory.getInstance().getUserInfo(UserOnlineImpl.getInstance().get(token));
        if (user == null) {
            return AjaxResult.errorLogoutResult();
        }
        try {
            userCooperateApplyService.modifyStatus(id, 1, user.getUsername());
        } catch (Exception e) {
            e.printStackTrace();
            return AjaxResult.errorResult(e.getMessage());
        }
        return AjaxResult.successResult(null, 0);
    }

}
